package com.ruoyi.system.controller;

import java.util.*;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.impl.RenwuServiceImpl;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-06-19
 */
@Controller
@RequestMapping("/system/renwu")
public class RenwuController extends BaseController
{
    private String prefix = "system/renwu";

    @Autowired
    private IRenwuService renwuService;

    @Autowired
    private IZhuanjiabiandongshenbaoService zhuanjiabiandongshenbaoService;

    @Autowired
    private IShiwuService shiwuService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IZhuanjiaDeptService zhuanjiaDeptService;

    @Autowired
    private IZhuanjiaBiandonghistoryService zhuanjiaBiandonghistoryService;

    @Autowired
    private IZhuanjiaRenkouhechaService zhuanjiaRenkouhechaService;

    @Autowired
    private IZhuanjiaRenkouhechaPiciService zhuanjiaRenkouhechaPiciService;

    @RequiresPermissions("system:renwu:view")
    @GetMapping()
    public String renwu()
    {
        return prefix + "/renwu";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:renwu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Renwu renwu)
    {
        startPage();
        List<Renwu> list = renwuService.selectRenwuList(renwu);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:renwu:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Renwu renwu)
    {
        List<Renwu> list = renwuService.selectRenwuList(renwu);
        ExcelUtil<Renwu> util = new ExcelUtil<Renwu>(Renwu.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:renwu:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Renwu renwu)
    {
        return toAjax(renwuService.insertRenwu(renwu));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:renwu:edit")
    @GetMapping("/edit/{renwuid}")
    public String edit(@PathVariable("renwuid") String renwuid, ModelMap mmap)
    {
        Renwu renwu = renwuService.selectRenwuByRenwuid(renwuid);
        mmap.put("renwu", renwu);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:renwu:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Renwu renwu)
    {
        return toAjax(renwuService.updateRenwu(renwu));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:renwu:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(renwuService.deleteRenwuByRenwuids(ids));
    }

    // 审核
    @PostMapping("/submit_custom")
    @ResponseBody
    public JsonResult<?> submit_custom(@RequestParam("id") String ppId,
                                       @RequestParam("deptid") String ppDeptId,
                                       HttpServletRequest request) {
        try {
            // 查找当前审核单位的上级单位
            Dept mmdept = deptService.selectDeptByDeptid(ppDeptId);
            if(mmdept==null){
                return  JsonResult.getErrorResult("当前提交单位不存在 提交失败");
            }

            // 查询变动是否存在
            Zhuanjiabiandongshenbao mmONE = zhuanjiabiandongshenbaoService.selectZhuanjiabiandongshenbaoByShenbaoid(ppId);
            if(mmONE==null){
                return  JsonResult.getErrorResult("当前提交变动不存在");
            }
            if(!mmONE.getDeptid().equals(ppDeptId)){
                return  JsonResult.getErrorResult("当前提交变动单位有误");
            }

            // 查找事务是否存在
            Shiwu 查询条件 = new Shiwu();
            查询条件.setBeizhu1(ppId);
            List<Shiwu> mmshiwuList =shiwuService.selectShiwuList(查询条件);
            if(mmshiwuList.size()!=1){
                return  JsonResult.getErrorResult("当前事务有误 提交失败");
            }
            Shiwu mm提交事务 = mmshiwuList.get(0);
            if(mm提交事务.getIsqidong()!=(long)0){
                return  JsonResult.getErrorResult("当前事务已经启动请勿重复提交");
            }

            // 判断是否当前专家是否存在正在审核的事务
            List<Map<String,Object>> mmShiwuList = renwuService.existShenhezhongShiwuByZhuanjiaId(mmONE.getZhuanjiaid());
            if(mmShiwuList.size()>0){
                return JsonResult.getErrorResult("该专家有正在审核的变动，请勿重复提交");
            }
            if(istingfa(mmONE.getBiandongleixing())){
                Zhuanjia mm专家 = zhuanjiaService.selectZhuanjiaByZhuanjiaid(mmONE.getZhuanjiaid());
                if(mm专家 ==null){
                    return JsonResult.getErrorResult("该专家信息不存在");
                }else{
                    if(mm专家.getZhuanjiazhuangtai()!=null){
                        if(istingfa(mm专家.getZhuanjiazhuangtai())){
                            return JsonResult.getErrorResult("当前专家状态已是停发状态 请勿连续重复设置停发状态");
                        }
                    }
                }
            }

            //处理人口核查
            ZhuanjiaRenkouhecha mmhechatemp = new ZhuanjiaRenkouhecha();
            mmhechatemp.setBiandongid(ppId);
            List<ZhuanjiaRenkouhecha> mmTemplist = zhuanjiaRenkouhechaService.selectZhuanjiaRenkouhechaList(mmhechatemp);
            if (!mmTemplist.isEmpty()){
                if (mmTemplist.size()>1){
                    return JsonResult.getErrorResult("当前专家状态关联人口核查数据有误");
                }
                if (mmTemplist.size()==1){
                    ZhuanjiaRenkouhecha mmHecha = mmTemplist.get(0);
                    mmHecha.setChuliriqi(new Date());
                    mmHecha.setShifouchuli(1l);
                    mmHecha.setChulijieguo("提交去世变动");
                    zhuanjiaRenkouhechaService.updateZhuanjiaRenkouhecha(mmHecha);

                    String piciid = mmHecha.getPiciid();
                    ZhuanjiaRenkouhechaPici mmPici = zhuanjiaRenkouhechaPiciService.selectZhuanjiaRenkouhechaPiciById(piciid);
                    mmPici.setWeihecha(mmPici.getWeihecha()-1);
                    mmPici.setYihecha(mmPici.getYihecha()+1);
                    zhuanjiaRenkouhechaPiciService.updateZhuanjiaRenkouhechaPici(mmPici);
                }
            }

            // 事务重新赋值
            mm提交事务.setIsqidong((long)1);
            mm提交事务.setQidongshijian(new Date());

            // 创建审批任务
            Renwu mmOne = new Renwu();
            mmOne.setRenwuid(java.util.UUID.randomUUID().toString());
            mmOne.setShiwuid(mm提交事务.getShiwuid());
            mmOne.setShenpidanweiid(mmdept.getParentid());
            mmOne.setRenwuxiafashijian(new Date());
            mmOne.setShifouwancheng((long)0);
            mmOne.setIsvalid((long)1);
            mmOne.setOrdernum((long) renwuService.getMaxOrdernumByShiwuId(mm提交事务.getShiwuid()) + 1);

            return renwuService.submit_custom(mmOne, mm提交事务) == true ? JsonResult.getSuccessResult("提交成功") : JsonResult.getErrorResult("提交失败");
        } catch (Exception e) {
            logger.error("renwu -> submit_custom: " + e.getMessage());
            return JsonResult.getErrorResult("提交任务失败");
        }
    }

    @PostMapping("/findbdshenhelist")
    @ResponseBody
    public JsonResult<?> findbdshenhelist(@RequestParam("shenpideptid")String shenpideptid,
                                          @RequestParam("zhuangtai")String zhuangtai,
                                          @RequestParam("biandongleixing")String biandongleixing,
                                          @RequestParam("deptname")String deptname,
                                          @RequestParam("xingming")String xingming,
                                          @RequestParam("xiafashijiankaishi")String xiafashijiankaishi,
                                          @RequestParam("xiafashijianjieshu")String xiafashijianjieshu,
                                          @RequestParam("wanchengshijiankaishi")String wanchengshijiankaishi,
                                          @RequestParam("wanchengshijianjieshu")String wanchengshijianjieshu,
                                          @RequestParam("pageindex") int ppPageIndex,
                                          @RequestParam("pagesize") int ppPageSize,
                                          HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString();
            if(!deptid.equals(shenpideptid)){
                return JsonResult.getErrorResult("查询审核列表单位有误");
            }
            // 判断状态  0 未审核  1 通过  2驳回  9全部
            int shifouwancheng = 99;
            int shifoutongguo = 99;
            if(zhuangtai.equals("0")){
                shifouwancheng =0;
            }else if(zhuangtai.equals("1")){
                shifoutongguo = 1;
            }else if(zhuangtai.equals("2")){
                shifouwancheng =1;
                shifoutongguo = 2;
            }else if(zhuangtai.equals("9")){
               // 默认就是全部
            }else{
                return JsonResult.getErrorResult("查询审核列表类型有误");
            }
            List<Map<String,Object>> list = renwuService.findBdShenheListByDept(shenpideptid,shifoutongguo,shifouwancheng,biandongleixing,deptname,xingming,
                    xiafashijiankaishi,xiafashijianjieshu,wanchengshijiankaishi,wanchengshijianjieshu,ppPageIndex,ppPageSize);

            int mmCount = renwuService.countBdShenheListByDept(shenpideptid,shifoutongguo,shifouwancheng,biandongleixing,deptname,xingming,
                    xiafashijiankaishi,xiafashijianjieshu,wanchengshijiankaishi,wanchengshijianjieshu);

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("shenhelist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            logger.error("renwu -> findbdshenhelist: " + e.getMessage());
            return JsonResult.getErrorResult("查询审核列表失败");
        }
    }

    @PostMapping("/findspjl")
    @ResponseBody
    public JsonResult<?> findspjl(@RequestParam("shiwuid")String shiwuid) {
        try {
            return JsonResult.getSuccessResult(renwuService.findspjl(shiwuid));
        } catch (Exception e) {
            logger.error("renwu -> findspjl: " + e.getMessage());
            return JsonResult.getErrorResult("查询审核记录失败");
        }
    }


    // 审批
    @PostMapping("/shenpi")
    @ResponseBody
    public JsonResult<?> shenpi(@RequestParam("renwuid")String renwuid,
                                          @RequestParam("zhuangtai")int zhuangtai,
                                          @RequestParam("yijian")String yijian,
                                          HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            String userid = session.getAttribute("userid").toString(); //
            String username = session.getAttribute("username").toString(); // 单位id
            Dept mmdept = deptService.selectDeptByDeptid(deptid);
            if(mmdept==null){
                return  JsonResult.getErrorResult("当前提交单位不存在 提交失败");
            }
            // 查询当前任务
            Renwu mm审核任务 = renwuService.selectRenwuByRenwuid(renwuid);
            if(mm审核任务==null){
                return  JsonResult.getErrorResult("当前任务不存在");
            }else{
                if(!mm审核任务.getShenpidanweiid().equals(deptid)){
                    return  JsonResult.getErrorResult("当前审核单位有误");
                }
                if(mm审核任务.getShifouwancheng()!=null&&mm审核任务.getShifouwancheng()==(long)1){
                    return  JsonResult.getErrorResult("当前任务已审核");
                }
            }

            // 查找事务是否存在
            Shiwu mm提交事务 = shiwuService.selectShiwuByShiwuid(mm审核任务.getShiwuid());
            if(mm提交事务.getIsqidong()==(long)0||mm提交事务.getIsjieshu()==(long)1){
                return  JsonResult.getErrorResult("当前事务尚未启动或已经审核结束 审核失败");
            }

            // 查询变动是否存在
            Zhuanjiabiandongshenbao mmONE = zhuanjiabiandongshenbaoService.selectZhuanjiabiandongshenbaoByShenbaoid(mm提交事务.getBeizhu1());
            if(mmONE==null){
                return  JsonResult.getErrorResult("当前提交变动不存在");
            }
            if(mmONE.getBiandongleixing().equals("8")){
                return  JsonResult.getErrorResult("当前审批变动变动类型不存在");
            }
            mm审核任务.setShifouwancheng((long)1);
            mm审核任务.setRenwuwanchengshijian(new Date());
            mm审核任务.setShifoutongguo((long)zhuangtai);
            mm审核任务.setShenpiyijian(yijian);
            mm审核任务.setShenpirenid(userid);
            mm审核任务.setShenpiren(username);
            boolean isok = false;
            // 判断状态
            if(zhuangtai==0){ // 不通过 逐级驳回
                // 任务结束 重新创建一条任务
                String mm下一级单位 = deptService.returnxiaji(deptid,mmONE.getDeptid());

                if(mm下一级单位.equals(mmONE.getDeptid())){  // 如果审核单位的下一级就是提交单位  直接结束任务并修改事务为未启动
                    mm提交事务.setIsqidong((long)0);
                    mm提交事务.setQidongshijian(null);
                    isok = renwuService.shenpi(mm审核任务,null,mm提交事务,null);
                }else{  // 创建审批任务
                    Renwu mmaddOne = new Renwu();
                    mmaddOne.setRenwuid(java.util.UUID.randomUUID().toString());
                    mmaddOne.setShiwuid(mm提交事务.getShiwuid());
                    mmaddOne.setShenpidanweiid(mm下一级单位);
                    mmaddOne.setRenwuxiafashijian(new Date());
                    mmaddOne.setShifouwancheng((long)0);
                    mmaddOne.setIsvalid((long)1);
                    mmaddOne.setOrdernum((long) renwuService.getMaxOrdernumByShiwuId(mm提交事务.getShiwuid()) + 1);
                    isok = renwuService.shenpi(mm审核任务,mmaddOne,null,null);
                }

            }else if(zhuangtai==1){ // 状态为通过
                if(deptid.equals(RuoYiConfig.getZjzxdeptid())){// 通过 不再新增任务 进行专家信息操作

                   // 完成当前任务 修改专家信息
                    Zhuanjia mm专家 = zhuanjiaService.selectZhuanjiaByZhuanjiaid(mmONE.getZhuanjiaid());
                    if(mm专家==null){
                        return JsonResult.getErrorResult("专家信息查询失败");
                    }

                    //专家变动历史
                    ZhuanjiaBiandonghistory mmHistory = new ZhuanjiaBiandonghistory();
                    mmHistory.setId(UUID.randomUUID().toString());
                    mmHistory.setZhuanjiaid(mmONE.getZhuanjiaid());
                    mmHistory.setBiandongshijian(mmONE.getBiandongshijian());
                    mmHistory.setOldzhuantai(mm专家.getZhuanjiazhuangtai());
                    if(mmONE.getBiandongleixing().equals("7")||mmONE.getBiandongleixing().equals("9")){
                        mm专家.setZhuanjiazhuangtai("99"); // 如果是恢复发放  则专家状态变为正常
                        mmHistory.setNewzhuangtai("99");
                    }else{
                        mm专家.setZhuanjiazhuangtai(mmONE.getBiandongleixing());
                        mmHistory.setNewzhuangtai(mmONE.getBiandongleixing());
                    }
                    mmHistory.setIsvalid(1l);
                    //添加变动历史
                    zhuanjiaBiandonghistoryService.insertZhuanjiaBiandonghistory(mmHistory);

                    //核增，需要添加单位专家关系表
                    if(mmONE.getBiandongleixing().equals("9")){
                        ZhuanjiaDept zd = new ZhuanjiaDept();
                        zd.setId(UUID.randomUUID().toString());
                        zd.setZhuanjiaid(mmONE.getZhuanjiaid());
                        zd.setDeptid(mmONE.getDeptid());
                        zd.setXingming(mmONE.getXingming());
                        zd.setStatus("1");

                        zhuanjiaDeptService.insertZhuanjiaDept(zd);
                    }

                    mm专家.setZhuangtaibiandongshijian(mmONE.getBiandongshijian());
                    mm专家.setStatus(1l);
                    mm提交事务.setIsjieshu((long)1);
                    mm提交事务.setJieshushijian(new Date());

                    isok = renwuService.shenpi(mm审核任务,null,mm提交事务,mm专家);

                }else{ // 不是专家中心就继续添加审核任务
                    Renwu mmaddOne = new Renwu();
                    mmaddOne.setRenwuid(java.util.UUID.randomUUID().toString());
                    mmaddOne.setShiwuid(mm提交事务.getShiwuid());
                    mmaddOne.setShenpidanweiid(mmdept.getParentid());
                    mmaddOne.setRenwuxiafashijian(new Date());
                    mmaddOne.setShifouwancheng((long)0);
                    mmaddOne.setShangyijishenpidanweiid(deptid);
                    mmaddOne.setIsvalid((long)1);
                    mmaddOne.setOrdernum((long) renwuService.getMaxOrdernumByShiwuId(mm提交事务.getShiwuid()) + 1);
                    isok = renwuService.shenpi(mm审核任务,mmaddOne,null,null);
                }

            }
            if(isok){
                return JsonResult.getSuccessResult("审核成功");
            }else{
                return JsonResult.getErrorResult("审核失败");
            }

        } catch (Exception e) {
            logger.error("renwu -> shenpi: " + e.getMessage());
            return JsonResult.getErrorResult("审核失败");
        }
    }


    // 转隶审批
    @PostMapping("/shenpi_zl")
    @ResponseBody
    public JsonResult<?> shenpi_zl(@RequestParam("renwuid")String renwuid,
                                   @RequestParam("zhuangtai")int zhuangtai,   // 0 驳回 1本单位管理  2提交上级 3转移到下级单位 4 正式接受
                                   @RequestParam("zhuanyideptid")String zhuanyideptid, // 转移单位id
                                   @RequestParam("yijian")String yijian,
                                   HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            String userid = session.getAttribute("userid").toString(); //
            String username = session.getAttribute("username").toString(); // 单位id
            String deptname = session.getAttribute("deptname").toString();
            Dept mmdept = deptService.selectDeptByDeptid(deptid);
            if(mmdept==null){
                return  JsonResult.getErrorResult("当前提交单位不存在 提交失败");
            }
            // 查询当前任务
            Renwu mm审核任务 = renwuService.selectRenwuByRenwuid(renwuid);
            if(mm审核任务==null){
                return  JsonResult.getErrorResult("当前任务不存在");
            }else{
                if(!mm审核任务.getShenpidanweiid().equals(deptid)){
                    return  JsonResult.getErrorResult("当前审核单位有误");
                }
                if(mm审核任务.getShifouwancheng()!=null&&mm审核任务.getShifouwancheng()==(long)1){
                    return  JsonResult.getErrorResult("当前任务已审核");
                }
            }

            // 查找事务是否存在
            Shiwu mm提交事务 = shiwuService.selectShiwuByShiwuid(mm审核任务.getShiwuid());
            if(mm提交事务.getIsqidong()==(long)0||mm提交事务.getIsjieshu()==(long)1){
                return  JsonResult.getErrorResult("当前事务尚未启动或已经审核结束 审核失败");
            }

            // 查询变动是否存在
            Zhuanjiabiandongshenbao mmONE = zhuanjiabiandongshenbaoService.selectZhuanjiabiandongshenbaoByShenbaoid(mm提交事务.getBeizhu1());
            if(mmONE==null){
                return  JsonResult.getErrorResult("当前提交变动不存在");
            }
            if(!mmONE.getBiandongleixing().equals("8")){  // 只有转隶
                return  JsonResult.getErrorResult("当前审批变动变动类型不存在");
            }

            mm审核任务.setShifouwancheng((long)1);
            mm审核任务.setRenwuwanchengshijian(new Date());
            if(zhuangtai==0){
                mm审核任务.setShifoutongguo((long)zhuangtai);
            }else{
                mm审核任务.setShifoutongguo((long)1); // 除了驳回 其余都是通过
            }
            mm审核任务.setBeizhu1(zhuangtai+""); // 转隶存 审批类型
            mm审核任务.setShenpiyijian(yijian);
            mm审核任务.setShenpirenid(userid);
            mm审核任务.setShenpiren(username);
            boolean isok = false;
            // 判断状态
            if(zhuangtai==0){ // 不通过 逐级驳回
                // 任务结束 重新创建一条任务   判断当前单位是否是提交单位的上级 是就查下级单位id 不是就往上级驳回
                Boolean Is上级 = deptService.returnshangji(mmONE.getDeptid(),deptid);
                String mm下一级单位 ="";
                if(Is上级){
                    mm下一级单位 = deptService.returnxiaji(deptid,mmONE.getDeptid());
                }else{
                    mm下一级单位 =mmdept.getParentid(); // 往上级驳回
                }

                if(mm下一级单位.equals(mmONE.getDeptid())){  // 如果审核单位的下一级就是提交单位  直接结束任务并修改事务为未启动
                    mm提交事务.setIsqidong((long)0);
                    mm提交事务.setQidongshijian(null);
                    isok = renwuService.shenpiZl(mm审核任务,null,mm提交事务,null,null,null,null);
                }else{  // 创建审批任务
                    Renwu mmaddOne = new Renwu();
                    mmaddOne.setRenwuid(java.util.UUID.randomUUID().toString());
                    mmaddOne.setShiwuid(mm提交事务.getShiwuid());
                    mmaddOne.setShenpidanweiid(mm下一级单位);
                    mmaddOne.setRenwuxiafashijian(new Date());
                    mmaddOne.setShifouwancheng((long)0);
                    mmaddOne.setIsvalid((long)1);
                    mmaddOne.setOrdernum((long) renwuService.getMaxOrdernumByShiwuId(mm提交事务.getShiwuid()) + 1);
                    isok = renwuService.shenpiZl(mm审核任务,mmaddOne,null,null,null,null,null);
                }

            }else if(zhuangtai==1){ // 本单位管理 不再新增任务 进行专家单位信息操作

                Renwu mmaddOne = new Renwu();
                mmaddOne.setRenwuid(java.util.UUID.randomUUID().toString());
                mmaddOne.setShiwuid(mm提交事务.getShiwuid());
                mmaddOne.setShenpidanweiid(deptid); // 重新创建一个本单位任务
                mmaddOne.setRenwuxiafashijian(new Date());
                mmaddOne.setShifouwancheng((long)0);
                mmaddOne.setIsvalid((long)1);
                mmaddOne.setBeizhu1("4");
                mmaddOne.setBeizhu2(deptid);
                mmaddOne.setOrdernum((long) renwuService.getMaxOrdernumByShiwuId(mm提交事务.getShiwuid()) + 1);

                isok = renwuService.shenpiZl(mm审核任务,mmaddOne,null,null,null,null,null);

            }else if(zhuangtai==2){ // 提交上级 新增任务

                Renwu mmaddOne = new Renwu();
                mmaddOne.setRenwuid(java.util.UUID.randomUUID().toString());
                mmaddOne.setShiwuid(mm提交事务.getShiwuid());
                mmaddOne.setShenpidanweiid(mmdept.getParentid());
                mmaddOne.setRenwuxiafashijian(new Date());
                mmaddOne.setShifouwancheng((long)0);
                mmaddOne.setIsvalid((long)1);
                mmaddOne.setOrdernum((long) renwuService.getMaxOrdernumByShiwuId(mm提交事务.getShiwuid()) + 1);
                isok = renwuService.shenpiZl(mm审核任务,mmaddOne,null,null,null,null,null);
            } else if (zhuangtai==3) {// 转移下级 新增任务
                if(zhuanyideptid==null||zhuanyideptid.equals("")){
                    return JsonResult.getErrorResult("转移单位不能为空");
                }
                Boolean Is上级 = deptService.returnshangji(zhuanyideptid,deptid);
                if(!Is上级){
                    return JsonResult.getErrorResult("转移单位不是当前审批单位下级 审核失败");
                }
                Renwu mmaddOne = new Renwu();
                mmaddOne.setRenwuid(java.util.UUID.randomUUID().toString());
                mmaddOne.setShiwuid(mm提交事务.getShiwuid());
                mmaddOne.setShenpidanweiid(zhuanyideptid);
                mmaddOne.setRenwuxiafashijian(new Date());
                mmaddOne.setShifouwancheng((long)0);
                mmaddOne.setIsvalid((long)1);
                mmaddOne.setOrdernum((long) renwuService.getMaxOrdernumByShiwuId(mm提交事务.getShiwuid()) + 1);
                isok = renwuService.shenpiZl(mm审核任务,mmaddOne,null,null,null,null,null);
            }else if(zhuangtai==4){// 本单位正式接受
                ZhuanjiaDept mm更新条件 = new ZhuanjiaDept();
                ZhuanjiaDept mm添加专家 = new ZhuanjiaDept();
                mm更新条件.setZhuanjiaid(mmONE.getZhuanjiaid());
                mm更新条件.setStatus("1");
                List<ZhuanjiaDept> mmlist = zhuanjiaDeptService.selectZhuanjiaDeptList(mm更新条件);
                if(mmlist.size()!=1){
                    return JsonResult.getErrorResult("专家所属单位信息有误 审核失败");
                }
                // 旧的专家单位关系设置为无效
                mm更新条件 = mmlist.get(0);
                mm更新条件.setStatus("2");
                mm更新条件.setBiangengshijian(new Date());

                // 新的专家单位关系
                mm添加专家.setStatus("1");
                mm添加专家.setId(UUID.randomUUID().toString());
                String new专家id = UUID.randomUUID().toString();
                mm添加专家.setZhuanjiaid(new专家id);
                mm添加专家.setDeptid(deptid); // 当前审批单位就是新的所属单位
                mm添加专家.setXingming(mmlist.get(0).getXingming());
                mm添加专家.setDanweimingcheng(deptname);
                // 结束事务
                mm提交事务.setJieshushijian(new Date());
                mm提交事务.setIsjieshu((long)1);

                // 查询当前的专家
                Zhuanjia mm专家 = zhuanjiaService.selectZhuanjiaByZhuanjiaid(mmONE.getZhuanjiaid());
                if(mm专家==null){
                    return JsonResult.getErrorResult("专家信息查询失败");
                }
                Zhuanjia mmNew专家 = new Zhuanjia();  // 新专家
                // 旧的专家设置转隶状态
                mm专家.setZhuanjiazhuangtai("8");
                mm专家.setZhuangtaibiandongshijian(mmONE.getBiandongshijian());

                // 复制的专家使用新的专家id
                mmNew专家.setZhuanjiaid(new专家id);
                mmNew专家.setXingming(mm专家.getXingming());
                mmNew专家.setZhengjianleixing(mm专家.getZhengjianleixing());
                mmNew专家.setZhengjianhaoma(mm专家.getZhengjianhaoma());
                mmNew专家.setXingbie(mm专家.getXingbie());
                mmNew专家.setChushengriqi(mm专家.getChushengriqi());
                mmNew专家.setGuoji(mm专家.getGuoji());
                mmNew专家.setRuxuannianfen(mm专家.getRuxuannianfen());
                mmNew专家.setGongzuodanwei(mm专家.getGongzuodanwei());
                mmNew专家.setGongzuozhuangtai(mm专家.getGongzuozhuangtai());
                mmNew专家.setSuoshuhangye(mm专家.getSuoshuhangye());
                mmNew专家.setYoubian(mm专家.getYoubian());
                mmNew专家.setShoujihaoma(mm专家.getShoujihaoma());
                mmNew专家.setDianhuahaoma(mm专家.getDianhuahaoma());
                mmNew专家.setEmail(mm专家.getEmail());
                mmNew专家.setDizhi(mm专家.getDizhi());
                mmNew专家.setZhuanjiazhuangtai("99");
                mmNew专家.setZhuangtaibiandongshijian(mmONE.getBiandongshijian());
                mmNew专家.setZuihoubokuanshijian(mm专家.getZuihoubokuanshijian());
                mmNew专家.setLianxiren1(mm专家.getLianxiren1());
                mmNew专家.setLianxidianhua1(mm专家.getLianxidianhua1());
                mmNew专家.setGuanxi1(mm专家.getGuanxi1());
                mmNew专家.setLianxiren2(mm专家.getLianxiren2());
                mmNew专家.setLianxidianhua2(mm专家.getLianxidianhua2());
                mmNew专家.setGuanxi2(mm专家.getGuanxi2());
                mmNew专家.setLianxiren3(mm专家.getLianxiren3());
                mmNew专家.setLianxidianhua3(mm专家.getLianxidianhua3());
                mmNew专家.setGuanxi3(mm专家.getGuanxi3());
                mmNew专家.setYinhangmingcheng(mm专家.getYinhangmingcheng());
                mmNew专家.setLianhanghao(mm专家.getLianhanghao());
                mmNew专家.setStatus(1l);
                mmNew专家.setBeizhu1(mm专家.getBeizhu1());
                mmNew专家.setBeizhu2(mm专家.getBeizhu2());
                mmNew专家.setBeizhu3(mm专家.getBeizhu3());
                mmNew专家.setBeizhu4(mm专家.getBeizhu4());
                mmNew专家.setBeizhu5(mm专家.getBeizhu5());
                mmNew专家.setBeizhu6(mm专家.getBeizhu6());
                mmNew专家.setBeizhu7(mm专家.getBeizhu7());
                mmNew专家.setBeizhu8(mm专家.getBeizhu8());
                mmNew专家.setBeizhu9(mm专家.getBeizhu9());
                mmNew专家.setBeizhu10(mmONE.getZhuanjiaid()); // 将转隶前的专家id 放入表里

                //旧专家变动历史
                ZhuanjiaBiandonghistory mmHistory = new ZhuanjiaBiandonghistory();
                mmHistory.setId(UUID.randomUUID().toString());
                mmHistory.setZhuanjiaid(mmONE.getZhuanjiaid());
                mmHistory.setBiandongshijian(mmONE.getBiandongshijian());
                mmHistory.setOlddanwei(mmONE.getDeptid());
                mmHistory.setNewdanwei(deptid);
                mmHistory.setIsvalid(1l);
                //添加变动历史
                zhuanjiaBiandonghistoryService.insertZhuanjiaBiandonghistory(mmHistory);

                //新专家变动历史
                ZhuanjiaBiandonghistory mmHistory2 = new ZhuanjiaBiandonghistory();
                mmHistory2.setId(UUID.randomUUID().toString());
                mmHistory2.setZhuanjiaid(new专家id);
                mmHistory2.setBiandongshijian(mmONE.getBiandongshijian());
                mmHistory2.setOlddanwei(mmONE.getDeptid());
                mmHistory2.setNewdanwei(deptid);
                mmHistory2.setIsvalid(1l);
                //添加变动历史
                zhuanjiaBiandonghistoryService.insertZhuanjiaBiandonghistory(mmHistory2);

                isok = renwuService.shenpiZl(mm审核任务,null,mm提交事务,mm更新条件,mm添加专家,mm专家,mmNew专家);
            }else {
                return JsonResult.getErrorResult("状态有误 审核失败");
            }

            if(isok){
                return JsonResult.getSuccessResult("审核成功");
            }else{
                return JsonResult.getErrorResult("审核失败");
            }

        } catch (Exception e) {
            logger.error("renwu -> shenpi_zl: " + e.getMessage());
            return JsonResult.getErrorResult("审核失败");
        }
    }

    public boolean istingfa(String ppleixing){
        if (ppleixing.equals("1") || ppleixing.equals("2") || ppleixing.equals("3") ||
                ppleixing.equals("4") || ppleixing.equals("5") || ppleixing.equals("6")) {
            return true;
        }
        return false;
    }


}
